package com.bestcem.xm.txtanls.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.ObjectId;
import cn.hutool.core.lang.Validator;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.txtanls.dao.IndustryToKeywordDao;
import com.bestcem.xm.txtanls.dao.mapper.IndustryToKeywordMapper;
import com.bestcem.xm.txtanls.entity.mysql.IndustryKeywordEntity;
import com.bestcem.xm.txtanls.entity.pojo.IndustryKeywordDO;
import com.bestcem.xm.txtanls.enums.IndustryKeyWordStatusEnum;
import com.bestcem.xm.txtanls.grpc.v1.enums.KeywordStatusEnum;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liheng
 * @version v1.0
 * @date 2021/6/28 14:17
 */
@Repository
public class IndustryToKeywordDaoImpl implements IndustryToKeywordDao {

    @Resource
    private IndustryToKeywordMapper industryToKeywordMapper;


    /**
     * 根据组织id和行业id查询行业关键词
     *
     * @param orgId      组织id
     * @param industryId 行业id
     * @return 行业关键词
     * @author qiongyu.cui
     * @date 2021/09/27 13:09
     */
    @Override
    public List<IndustryKeywordDO> findOrgIndustryKeyword(String orgId, String industryId) {
        List<IndustryKeywordEntity> industryKeywordEntities = industryToKeywordMapper.findOrgIndustryKeyword(orgId, industryId);
        List<IndustryKeywordDO> industryKeywordDOS = new ArrayList<>();
        for (IndustryKeywordEntity industryKeywordEntity : industryKeywordEntities) {
            IndustryKeywordDO industryKeywordDO = toIndustryKeywordDO(industryKeywordEntity);
            industryKeywordDOS.add(industryKeywordDO);
        }
        return  industryKeywordDOS;
    }

    @Override
    public Set<String> listIndustryKeyword(String orgId, String industryId) {
        List<IndustryKeywordEntity> industryKeywordEntities = industryToKeywordMapper.findOrgIndustryKeyword(orgId, industryId);
        if(CollUtil.isEmpty(industryKeywordEntities)) {
            return new HashSet<>();
        }
        return industryKeywordEntities.stream()
                .filter(item -> Validator.isNotEmpty(item.getKeyword()) && item.getStatus() == KeywordStatusEnum.KeywordStatus.DISPLAY_VALUE)
                .map(IndustryKeywordEntity::getKeyword)
                .collect(Collectors.toSet());
    }

    /**
     * 批量存储组织的行业关键字
     *
     * @param orgId      组织id
     * @param industryId 行业id
     * @param keywords   行业关键词列表
     * @return 行业关键词列表
     */
    @Override
    public List<IndustryKeywordDO> insertOrgIndustryKeyword(String orgId, String industryId, Collection<String> keywords) {
        List<IndustryKeywordDO> industryKeywordDOS = new ArrayList<>();
        List<IndustryKeywordEntity> industryKeywordEntities = new ArrayList<>();
        int status = IndustryKeyWordStatusEnum.DISPLAY.getIndex();
        for (String keyword : keywords) {
            industryKeywordEntities.add(new IndustryKeywordEntity(ObjectId.next(), orgId, industryId, keyword, status,
                    null, DateUtil.getCommonDate(), null, DateUtil.getCommonDate(), false));
        }
        industryToKeywordMapper.batchInsertOrgIndustryKeyword(industryKeywordEntities);
        for (IndustryKeywordEntity industryKeywordEntity : industryKeywordEntities) {
            IndustryKeywordDO industryKeywordDO = toIndustryKeywordDO(industryKeywordEntity);
            industryKeywordDOS.add(industryKeywordDO);
        }
        return industryKeywordDOS;
    }

    @Override
    public void insertEntity(IndustryKeywordDO industryKeyword) {
        IndustryKeywordEntity industryKeywordEntity = toIndustryKeywordDO(industryKeyword);
        industryToKeywordMapper.insertEntity(industryKeywordEntity);
    }

    /**
     * industryKeywordEntity 转为 IndustryKeywordDO
     * @param industryKeywordEntity 实体
     * @return IndustryKeywordDO do
     * @author jy.zhao
     * @date 2021/9/28 14:55
     **/
    private IndustryKeywordDO toIndustryKeywordDO(IndustryKeywordEntity industryKeywordEntity) {
        IndustryKeywordDO industryKeywordDO = new IndustryKeywordDO();
        industryKeywordDO.setId(industryKeywordEntity.getIndustryKeywordId());
        industryKeywordDO.setOrgId(industryKeywordEntity.getOrgId());
        industryKeywordDO.setIndustryId(industryKeywordEntity.getIndustryId());
        industryKeywordDO.setKeyContent(industryKeywordEntity.getKeyword());
        industryKeywordDO.setStatus(industryKeywordEntity.getStatus());
        industryKeywordDO.setCreatedId(industryKeywordEntity.getCreatorId());
        industryKeywordDO.setCreatedDate(industryKeywordEntity.getCreateTime());
        industryKeywordDO.setUpdatedId(industryKeywordEntity.getOperatorId());
        industryKeywordDO.setUpdatedDate(industryKeywordEntity.getUpdateTime());
        industryKeywordDO.setSample(industryKeywordEntity.getSample());
        return industryKeywordDO;
    }
    private IndustryKeywordEntity toIndustryKeywordDO(IndustryKeywordDO industryKeywordDO) {
        IndustryKeywordEntity industryKeywordEntity = new IndustryKeywordEntity();
        industryKeywordEntity.setIndustryKeywordId(industryKeywordDO.getId());
        industryKeywordEntity.setOrgId(industryKeywordDO.getOrgId());
        industryKeywordEntity.setIndustryId(industryKeywordDO.getIndustryId());
        industryKeywordEntity.setKeyword(industryKeywordDO.getKeyContent());
        industryKeywordEntity.setStatus(industryKeywordDO.getStatus());
        industryKeywordEntity.setCreatorId(industryKeywordDO.getCreatedId());
        industryKeywordEntity.setCreateTime(industryKeywordDO.getCreatedDate());
        industryKeywordEntity.setOperatorId(industryKeywordDO.getUpdatedId());
        industryKeywordEntity.setUpdateTime(industryKeywordDO.getUpdatedDate());
        industryKeywordEntity.setSample(industryKeywordDO.getSample());
        return industryKeywordEntity;
    }

}
